<template>
    <div class="navi-list">
        <h3>导航列表</h3>
        <!-- :data="filterTableData" -->
        <el-table :data="tableData" style="width: 100%">
            <el-table-column label="ID" prop="id" />
            <el-table-column label="名称" prop="nav_name" />
            <el-table-column label="创建时间" prop="nav_create_time" />
            <el-table-column align="right">
                <template #header>
                    <el-input v-model="search" size="small" placeholder="搜索" />
                </template>
                <template #default="scope">
                    <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue'
import { listNavFunc,removeNavFunc } from '../../api/request'
import { Navi } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
// 定义接口，形容对象的结构
interface Navi {
    id?:number|string
    nav_name: string
    nav_id: string
    nav_create_time: string
}

const search = ref('')
const handleEdit = (index: number, row: Navi) => {
    // console.log(index, row)
}
const handleDelete = (index: number, row: Navi) => {
    // console.log(index, row)
    let {id,nav_name} = row;
    // 二次确认删除
    ElMessageBox.confirm(`确定要删除“${nav_name}”吗？`, '提示', {
        type: 'warning'
    })
    .then(() => {
        // 删除数据
        removeNavFunc({id})
        .then(
            (content: any)=>{
                ElMessage.success(content.data.msg);
                tableData.value.splice(index, 1);
                // 重新初始化
                naviListFn();
            },
            err=>{
                ElMessage.error(err.msg);
            }
        )
       
      
    })
    .catch(() => { });
}

// 用户列表数据
const tableData: Navi[] = ref([])

// 定义函数获取用户列表
const naviListFn = async ()=> {
    // let token = localStorage.getItem("TOKEN")
    await listNavFunc().then(
        (content:any)=>{
            // console.log(content)
            let {result, code} = content.data;
            // console.log(result,code)
            if(code == 200) {
                tableData.value = result;
            }
        }
    )
}

// 
naviListFn();

</script>

<style lang="scss">
.navi-list {
    h3 {
        margin-bottom: 15px;
    }
}
</style>